package com.example.kotlinlearn

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.content.edit
import com.bumptech.glide.Glide
import com.example.kotlinlearn.databinding.ActivityLoginMainBinding
import com.example.kotlinlearn.web.LoginWebService
import com.example.kotlinlearn.web.ServiceCreatorForWeb
import com.example.kotlinlearn.web.adminLogin
import com.example.kotlinlearn.web.responseData
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import java.util.*

class LoginActivity : BaseActivity() {

    lateinit var binding: ActivityLoginMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val prefs = getSharedPreferences("hdafbs",Context.MODE_PRIVATE)
        val password = prefs.getString("a"," ")
        if (password!=" "){
            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
        }
        binding.loginButton.setOnClickListener {
            login()
        }
        Glide.with(this).load("http://47.94.135.113:8081/captcha?time="+Date()).into(binding.codeImg)
        binding.codeImg.setOnClickListener {
            Glide.with(this).load("http://47.94.135.113:8081/captcha?time="+Date()).into(binding.codeImg)
        }
    }

    private fun login(){
        if (binding.accountInput.text.isNullOrEmpty()||binding.passwordInput.text.isNullOrEmpty()){
            Toast.makeText(this,"请输入账号密码",Toast.LENGTH_SHORT).show()
            return
        }
        binding.progressBar.visibility = View.VISIBLE
        val loginWeb = ServiceCreatorForWeb.create<LoginWebService>()
        val username = binding.accountInput.text.toString()
        val password = binding.passwordInput.text.toString()
        val code = binding.codeInput.text.toString()
        val adminLogin = adminLogin(username,password,code)
        loginWeb.login(adminLogin).enqueue(object :retrofit2.Callback<responseData>{
            override fun onResponse(call: Call<responseData>, response: Response<responseData>) {
                Log.d("TAG", "onResponse: ${response.body()}")
                startMainActivity(adminLogin)
            }

            override fun onFailure(call: Call<responseData>, t: Throwable) {
                TODO("Not yet implemented")
            }
        })
    }
    private fun startMainActivity(adminLogin: adminLogin){
        if (binding.rememberAccountCheck.isChecked){
            getSharedPreferences("hdafbs", Context.MODE_PRIVATE).edit{
                putString("a",adminLogin.password)
                putString("b",adminLogin.username)
            }
        }
        runOnUiThread {
            binding.progressBar.visibility = View.INVISIBLE
            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
        }
    }
}